﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;

namespace SportManager.TrainingWizard
{
    public class SlideC : SlickInterface.Slide
    {
        private Button button6;
        private TableLayoutPanel tableLayoutPanel1;
        private Button button5;
        private Label label3;
        private Label label2;
        private Label label4;
        private Button button1;
        private Label label5;
        private Button button3;
        private Training m_training;
        private ProgressBar progressBar1;
        private int currentExercise = 0;
        public bool IsPlanningTraining = false;

        public SlideC(ref Training training, bool isPlanningTraining)
        {
            m_training = training;
            this.InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.button6 = new System.Windows.Forms.Button();
            this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
            this.button5 = new System.Windows.Forms.Button();
            this.label3 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.button1 = new System.Windows.Forms.Button();
            this.label5 = new System.Windows.Forms.Label();
            this.button3 = new System.Windows.Forms.Button();
            this.progressBar1 = new System.Windows.Forms.ProgressBar();
            this.SuspendLayout();
            // 
            // button6
            // 
            this.button6.Enabled = false;
            this.button6.Location = new System.Drawing.Point(423, 83);
            this.button6.Name = "button6";
            this.button6.Size = new System.Drawing.Size(105, 24);
            this.button6.TabIndex = 29;
            this.button6.Text = "Удалить подход";
            this.button6.UseVisualStyleBackColor = true;
            this.button6.Click += new System.EventHandler(this.button6_Click);
            // 
            // tableLayoutPanel1
            // 
            this.tableLayoutPanel1.AutoScroll = true;
            this.tableLayoutPanel1.ColumnCount = 4;
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
            this.tableLayoutPanel1.Location = new System.Drawing.Point(110, 53);
            this.tableLayoutPanel1.Name = "tableLayoutPanel1";
            this.tableLayoutPanel1.RowCount = 1;
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle());
            this.tableLayoutPanel1.Size = new System.Drawing.Size(307, 210);
            this.tableLayoutPanel1.TabIndex = 28;
            // 
            // button5
            // 
            this.button5.Enabled = false;
            this.button5.Location = new System.Drawing.Point(423, 53);
            this.button5.Name = "button5";
            this.button5.Size = new System.Drawing.Size(105, 24);
            this.button5.TabIndex = 27;
            this.button5.Text = "Добавить подход";
            this.button5.UseVisualStyleBackColor = true;
            this.button5.Click += new System.EventHandler(this.button5_Click);
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(107, 10);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(121, 13);
            this.label3.TabIndex = 26;
            this.label3.Text = "Название упражнения";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(215, 34);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(26, 13);
            this.label2.TabIndex = 25;
            this.label2.Text = "Вес";
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(122, 34);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(52, 13);
            this.label4.TabIndex = 24;
            this.label4.Text = "Повторы";
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(430, 236);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(97, 37);
            this.button1.TabIndex = 30;
            this.button1.Text = "Следующее упражнение";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(281, 34);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(40, 13);
            this.label5.TabIndex = 31;
            this.label5.Text = "Время";
            // 
            // button3
            // 
            this.button3.Location = new System.Drawing.Point(3, 236);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(94, 37);
            this.button3.TabIndex = 32;
            this.button3.Text = "Предыдущее упражнение";
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new System.EventHandler(this.button3_Click);
            // 
            // progressBar1
            // 
            this.progressBar1.Location = new System.Drawing.Point(110, 269);
            this.progressBar1.Name = "progressBar1";
            this.progressBar1.Size = new System.Drawing.Size(307, 23);
            this.progressBar1.TabIndex = 33;
            // 
            // SlideC
            // 
            this.Controls.Add(this.progressBar1);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.button6);
            this.Controls.Add(this.tableLayoutPanel1);
            this.Controls.Add(this.button5);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label4);
            this.Name = "SlideC";
            this.Size = new System.Drawing.Size(530, 300);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            SaveApproachs();
            this.SlideControl.ForwardTo<SlideD>();
        }

        public void ShowApproachs()
        {
            progressBar1.Maximum = m_training.Exercises.Count;
            if (m_training.Exercises.Count > 0)
            {
                progressBar1.Value = currentExercise + 1;

                button5.Enabled = true;
                ClearExerciseLayouPanel();
                ExerciseInfo info = Program.ExercisesKeeper.FindItemByGuid(m_training.Exercises[currentExercise].Id);
                label3.Text = info.Name;
                List<Approach> approachs;

                if (IsPlanningTraining)
                {
                    approachs = m_training.Exercises[currentExercise].PlanningApproachs;
                }
                else
                {
                    approachs = m_training.Exercises[currentExercise].Approachs;
                }

                for (int i = 0; i < approachs.Count; i++)
                {
                    tableLayoutPanel1.Controls.Add(new TextBox(), 0, i);
                    if (info.IsRepeatsRequired)
                    {
                        tableLayoutPanel1.Controls[tableLayoutPanel1.Controls.Count - 1].Text = approachs[i].Repeats.ToString();
                        tableLayoutPanel1.Controls[tableLayoutPanel1.Controls.Count - 1].LostFocus += new EventHandler(EditTrainingDlg_LostFocus);
                    }
                    else
                    {
                        tableLayoutPanel1.Controls[tableLayoutPanel1.Controls.Count - 1].Enabled = false;
                    }

                    tableLayoutPanel1.Controls.Add(new TextBox(), 1, i);
                    if (info.IsWeightRequired)
                    {
                        tableLayoutPanel1.Controls[tableLayoutPanel1.Controls.Count - 1].Text = approachs[i].Weight.ToString();
                        tableLayoutPanel1.Controls[tableLayoutPanel1.Controls.Count - 1].LostFocus += new EventHandler(EditTrainingDlg_LostFocus);
                    }
                    else
                    {
                        tableLayoutPanel1.Controls[tableLayoutPanel1.Controls.Count - 1].Enabled = false;
                    }

                    tableLayoutPanel1.Controls.Add(new TextBox(), 2, i);
                    if (info.IsTimeSpanRequired)
                    {
                        tableLayoutPanel1.Controls[tableLayoutPanel1.Controls.Count - 1].Text = approachs[i].Time.ToString();
                        tableLayoutPanel1.Controls[tableLayoutPanel1.Controls.Count - 1].LostFocus += new EventHandler(EditTrainingDlg_LostFocus);
                    }
                    else
                    {
                        tableLayoutPanel1.Controls[tableLayoutPanel1.Controls.Count - 1].Enabled = false;
                    }

                    CheckBox checkBox = new CheckBox();
                    checkBox.CheckedChanged += new EventHandler(checkBox_CheckedChanged);
                    tableLayoutPanel1.Controls.Add(checkBox, 3, i);
                }
            }
        }

        private void checkBox_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox checkBox = (CheckBox)sender;
            if (checkBox.Checked)
            {
                button6.Enabled = true;
            }
        }

        private void EditTrainingDlg_LostFocus(object sender, EventArgs e)
        {
            SaveApproachs();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            List<int> array = new List<int>();
            if (MessageBox.Show("Вы уверены ?", "Удаление", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                List<Approach> approachs;
                if (IsPlanningTraining)
                {
                    approachs = m_training.Exercises[currentExercise].PlanningApproachs;
                }
                else
                {
                    approachs = m_training.Exercises[currentExercise].Approachs;
                }

                for (int i = 0; i < approachs.Count; i++)
                {
                    CheckBox checkBox = (CheckBox)tableLayoutPanel1.GetControlFromPosition(3, i);
                    if (checkBox.Checked)
                    {
                        array.Add(i);
                    }
                }

                for (int i = array.Count - 1; i >= 0; i--)
                {
                    approachs.RemoveAt((int)array[i]);
                    for (int j = 0; j < 3; j++)
                    {
                        tableLayoutPanel1.Controls.Remove(tableLayoutPanel1.GetControlFromPosition(j, (int)array[i]));
                    }
                }

                if (array.Count == 0)
                {
                    MessageBox.Show("Не выбран ни один подход", "Ошибка", MessageBoxButtons.OK);
                    button6.Enabled = false;
                }

                ShowApproachs();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (currentExercise < (m_training.Exercises.Count - 1))
            {
                SaveApproachs();
                currentExercise++;
                ShowApproachs();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (currentExercise > 0)
            {
                SaveApproachs();
                currentExercise--;
                ShowApproachs();
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            SaveApproachs();
            if (IsPlanningTraining)
            {
                m_training.Exercises[currentExercise].PlanningApproachs.Add(new Approach());
            }
            else
            {
                m_training.Exercises[currentExercise].Approachs.Add(new Approach());
            }

            ShowApproachs();
        }

        private void ClearExerciseLayouPanel()
        {
            tableLayoutPanel1.Controls.Clear();
            label3.Text = "Название упражнения";
        }

        private void SaveApproachs()
        {
            System.Globalization.NumberFormatInfo numFormat = new System.Globalization.NumberFormatInfo();
            numFormat.NumberDecimalSeparator = ",";

            ExerciseInfo info = Program.ExercisesKeeper.FindItemByGuid(m_training.Exercises[currentExercise].Id);
            List<Approach> approachs;

            if (IsPlanningTraining)
            {
                approachs = m_training.Exercises[currentExercise].PlanningApproachs;
            }
            else
            {
                approachs = m_training.Exercises[currentExercise].Approachs;
            }

            for (int i = 0; i < approachs.Count; i++)
            {
                if (info.IsRepeatsRequired)
                {
                    int repeats = 0;
                    if (int.TryParse((tableLayoutPanel1.GetControlFromPosition(0, i).Text), out repeats))
                    {
                        approachs[i].Repeats = repeats;
                    }
                    else
                    {
                        tableLayoutPanel1.GetControlFromPosition(0, i).Text = "0";
                        MessageBox.Show("Неверное значение", "", MessageBoxButtons.OK);
                    }
                }

                if (info.IsWeightRequired)
                {
                    double weight = 0;
                    if (double.TryParse((tableLayoutPanel1.GetControlFromPosition(1, i).Text)
                        , System.Globalization.NumberStyles.Number, numFormat, out weight))
                    {
                        approachs[i].Weight = weight;
                    }
                    else
                    {
                        tableLayoutPanel1.GetControlFromPosition(1, i).Text = "0";
                        MessageBox.Show("Неверное значение", "", MessageBoxButtons.OK);
                    }
                }

                if (info.IsTimeSpanRequired)
                {
                    int time = 0;
                    if (int.TryParse((tableLayoutPanel1.GetControlFromPosition(2, i).Text), out time))
                    {
                        approachs[i].Time = time;
                    }
                    else
                    {
                        tableLayoutPanel1.GetControlFromPosition(2, i).Text = "0";
                        MessageBox.Show("Неверное значение", "", MessageBoxButtons.OK);
                    }
                }
            }
        }

    }
}
